﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BeatEmUp
{
    class MovementManager : IMovementManager
    {
        const int move = 5;
        public MovementManager(Rectangle rectangle)
        {
            position = rectangle;
        }
        
        private Rectangle position;

        private void MoveUp()
        {
            position.Y-=move;
        }
        private void MoveDown()
        {
            position.Y += move;
        }
        private void MoveLeft()
        {
            position.X -= move;
        }
        private void MoveRight()
        {
            position.X += move;
        }

        public void DoSomeMovement(KeyboardState keyboardState, GamePadState gamePadState)
        {
 	        if(keyboardState.IsKeyDown(Keys.Up)||
                gamePadState.IsButtonDown(Buttons.LeftThumbstickUp)||
                gamePadState.IsButtonDown(Buttons.DPadUp))
            {
                if (!MovementBoundaries.ExceedsBoundaries(new Rectangle(position.X, position.Y - move, position.Width, position.Height)))
                    MoveUp();
                else
                    position.Y = MovementBoundaries.gameField.Y;
            }
            if(keyboardState.IsKeyDown(Keys.Down)||
                gamePadState.IsButtonDown(Buttons.LeftThumbstickDown)||
                gamePadState.IsButtonDown(Buttons.DPadDown))
            {
                if (!MovementBoundaries.ExceedsBoundaries(new Rectangle(position.X, position.Y + move, position.Width, position.Height)))
                    MoveDown();
                else
                    position.Y = MovementBoundaries.gameField.Y + MovementBoundaries.gameField.Height - position.Height;
            }
            if(keyboardState.IsKeyDown(Keys.Left)||
                gamePadState.IsButtonDown(Buttons.LeftThumbstickLeft)||
                gamePadState.IsButtonDown(Buttons.DPadLeft))
            {
                if (!MovementBoundaries.ExceedsBoundaries(new Rectangle(position.X - move, position.Y, position.Width, position.Height)))
                    MoveLeft();
                else
                    position.X = MovementBoundaries.gameField.X;
            }
            if(keyboardState.IsKeyDown(Keys.Right)||
                gamePadState.IsButtonDown(Buttons.LeftThumbstickRight)||
                gamePadState.IsButtonDown(Buttons.DPadRight))
            {
                if (!MovementBoundaries.ExceedsBoundaries(new Rectangle(position.X + move, position.Y, position.Width, position.Height)))
                    MoveRight();
                else
                    position.X = MovementBoundaries.gameField.Width - position.Width;
            }

        }

        public Rectangle Position
        {
            get { return position; }
            set { position = value; }
        }

    }
}
